package cn.azzhu.day02.source;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

/**
 * 从kafka中读取数据的source，可以并行的Source，并且可以实现 ExactlyOnce语义
 * @author azzhu
 * @create 2020-09-18 00:16:22
 */
public class KafkaSource {
    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //kafkaSource
        final Properties props = new Properties();
        //指定kafka的broker地址
        props.setProperty("bootstrap.servers", "localhost:9092");
        //指定组ID
        props.setProperty("group.id", "test");
        //如果没有指定偏移量，第一次从最开始消费
        props.setProperty("auto.offset.reset","earliest");
        //kafka的消费者不自动提交偏移量
//        props.setProperty("enable.auto.commit","false");

        final FlinkKafkaConsumer<String> kafkaSource = new FlinkKafkaConsumer<>(
                "wc",
                new SimpleStringSchema(),
                props
        );

        //source
        final DataStream<String> lines = env.addSource(kafkaSource);

        //sink
        lines.print();

        env.execute("KafkaSource");
    }
}
